package com.liuzhen.servlet;

import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;

@WebServlet("/servlet11")
public class servlet11 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext servletContext = getServletContext();
        String key1 = getServletContext().getInitParameter("key1");
        System.out.println("key1:" + key1);
         /*
          String getRealPath("web目录下的web资源的相对路径")
                 根据相对路径获取web资源下的绝对路径

                 将来我们想获取部署到tomcat中web目录下的资源,假如我们忘记了tomcat在什么位置了,此时我们不能直接
                 写绝对路径,因为我们都不知道部署项目的位置,所以也就不能写绝对路径了
                 我们只能通过web资源的相对路径获取对应的绝对路径
         */
        String contextPath = servletContext.getRealPath("1.txt");
        System.out.println("contextPath:" + contextPath);

        String realPath = servletContext.getRealPath("WEB-INF/2.txt");
        System.out.println("realPath:" + realPath);

        String realPath1 = servletContext.getRealPath("WEB-INF/classes/com/3.txt");
        System.out.println("realPath1:" + realPath1);

        String contextPath1 = servletContext.getContextPath();
        System.out.println(contextPath1);

        String mimeType = servletContext.getMimeType("1.jpg");
        System.out.println("mimeType:" + mimeType);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}